<?php
/**
 * Created by PhpStorm.
 * User: osxfal
 * Date: 2019/2/23
 * Time: 下午7:03
 */

namespace app\index\controller;

use think\Controller;
use think\cache\driver\Redis;

/* 统计管理*/
class Statistic extends Controller{


    //判断用户是否登录
    public function __construct()
    {
        parent::__construct();
        if(empty(session('user')))
        {
//            $msg = [];
//            $msg['result'] = 3;//session判断专用值
//            $msg['tips'] = '对不起,您还没有登录!请先登录!';
//            $msg = json_encode($msg,320);
            $this->error('对不起,您还没有登录!请先登录!','/login/guard');
//            echo $msg;  //返回前端告诉前端跳转回登录页

        }

    }



    //渠道统计列表
    /* ID , 渠道名称  ， 点击数， UV,   注册数 ，  激活数，  激活率，    实名数，    实名率 */
    public function chanelStatisticList(){

        //参数接收

        $stime = input('get.stime');//开始时间
        $etime = input('get.etime');//结束时间
        $username = input('get.username'); //渠道商名
        $page = input('get.page');  //页数
        $lines = input('get.lines'); //行数
        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        if(empty($username) && empty($stime) && empty($etime)){
            $cached = new Redis();
            $cache_data = $cached->get('distributors_data');
            $cache_data = json_decode($cache_data,true);
            $limit_data = [];

            $top = $lines;

            $bid = ($page - 1) * $lines;  //下标起始位置；

            if(count($cache_data)<$lines){

                $top = count($cache_data);
            }
            for($i=0;$i<$top;$i++){
                if(!isset($cache_data[$bid]))  break;
                $limit_data[] = $cache_data[$bid];
                $bid+=1;//下标增加
            }

            $arr = [];
            $arr['rows'] = $limit_data;
            $arr['total'] = count($cache_data);
            $arr['result'] = 1;
            $arr['tips'] = '查询成功！';

            $arr = json_encode($arr,320);
            exit($arr);
        }


        $where = []; //初始化
        if($username){
            $where['username'] = ['LIKE','%'. $username . '%'];
        }



        /*时间转化测试  日期 -> 时间戳 */
//        $time_ = date('Y-m-d H:i:s',1533788245);//1533788245
//        $t2 = strtotime('2018-08-09 12:17:25');
//        echo $t2;die;

        $paras = []; //初始化 要传递的参数数组

        if(empty($stime)) $stime = date('Y-m-d');
        if(empty($etime)) $etime = date('Y-m-d');

        !empty($stime) && $paras['stime'] = $stime;
        !empty($etime) && $paras['etime'] = $etime;
        !empty($username) && $paras['username'] = $username;

        if($where){
            $result = db('admin')->field('uid,username')->where($where)->select();
        }
        else{
            $result = db('admin')->field('uid,username')->select();
        }




        foreach($result as &$v){  //循环结果 拿数据
            $this->pv($v,$paras);
        }


        foreach($result as &$v){
            $this->uv($v,$paras);
        }

        foreach($result as &$v){
            $this->regs($v,$paras);
        }

        foreach($result as &$v){
            $this->activation($v,$paras);
        }

        foreach($result as &$v){
            $this->real_name($v,$paras);
        }


        foreach($result as &$v){
            $this->active_rate($v);
        }


        foreach($result as &$v){
            $this->reals_rate($v);
        }

        //使用 for 进行分页 $limit = ($page - 1) * $lines . ',' . $lines;    //分页

        $bid = ($page - 1) * $lines;  //下标起始位置；
        $top = $lines;



        if(count($result) < $lines){
            $top = count($result);
        }
        $arr_paginate_data=[];
        for($i=0;$i<$top;$i++){


                if(!isset($result[$bid]))  break;
                $arr_paginate_data[$i] = $result[$bid];
                $bid+=1;



            //下标增加
        }


        $arr = [];
        $arr['rows'] = $arr_paginate_data;
        $arr['total'] = count($result);
        $arr['result'] = 1;
        $arr['tips'] = '查询成功！';

        $arr = json_encode($arr,320);
        echo $arr;

    }//chanel func

    //点击量
    public function pv(&$data,$paras){
        $code = $data['uid'];  //渠道商编号
        if(empty($paras['stime'])){
            //要转时间戳
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime($paras['stime']);
        }

        if(empty($paras['etime'])){
            //要转时间戳
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime($paras['etime']);
        }

        $where = [];
        $where['time'] = ['BETWEEN',[$paras['stime'],$paras['etime']]];
//        $where['time'] = ['elt',$paras['etime']];
        $where['code'] = ['eq',$code];

        $tmp = db('tongji')->field('COUNT(id) as pv,date')->where($where)->find();
//        echo db('tongji')->getLastSql();die;
        $data['pv'] = $tmp['pv'];
        $data['date'] = $tmp['date'];
    }




    //UV
    public function uv(&$data,$paras){
        $code = $data['uid'];  //渠道商编号
        if(empty($paras['stime'])){
            $paras['stime'] = date('Y-m-d');
        }

        if(empty($paras['etime'])){
            $paras['etime'] = date('Y-m-d');
        }

        $where = [];
        $where['date'] = ['BETWEEN',[$paras['stime'] ,$paras['etime'] ]];
//        $where['date'] = ['elt',$paras['etime']];
        $where['code'] = ['eq',$code]; //渠道商编号
        $where['sortcs'] = ['eq',1];  //表示第一次点击

        $tmp = db('tongji')->field('COUNT(DISTINCT ip) as uv')->where($where)->find();
//        echo db('tongji')->getLastSql();die;
        $data['uv'] = $tmp['uv'];
    }


    //注册数  reg
    public function regs(&$data,$paras){
        $diamond = $data['uid'];  //渠道商编号
        if(empty($paras['stime'])){
            //要转时间戳
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime($paras['stime']);
        }

        if(empty($paras['etime'])){
            //要转时间戳
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime($paras['etime']);
        }

        $where = [];
        $where['time'] = ['BETWEEN',[$paras['stime'],$paras['etime']]];
        $where['diamond'] = ['eq',$diamond]; //渠道商编号

        $tmp = db('member')->field('COUNT(uid) as regs_amount')->where($where)->find();
//        echo db('tongji')->getLastSql();die;
        $data['regs'] = $tmp['regs_amount'];
    }


    //激活
    public function activation(&$data,$paras){
        $diamond = $data['uid'];  //渠道商编号
        if(empty($paras['stime'])){
            //要转时间戳
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime($paras['stime']);
        }

        if(empty($paras['etime'])){
            //要转时间戳
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime($paras['etime']);
        }

        $where = [];
        $where['first_login_time'] = ['BETWEEN',[$paras['stime'],$paras['etime']]];
        $where['diamond'] = ['eq',$diamond]; //渠道商编号

        $tmp = db('member')->field('COUNT(uid) as act_amount')->where($where)->find();
//        echo db('tongji')->getLastSql();die;
        $data['acts'] = $tmp['act_amount'];
    }


    //实名
    public function real_name(&$data,$paras){
        $diamond = $data['uid'];  //渠道商编号
        if(empty($paras['stime'])){
            //要转时间戳
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['stime'] .= ' 00:00:00';
            $paras['stime'] = strtotime($paras['stime']);
        }

        if(empty($paras['etime'])){
            //要转时间戳
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime(date('Y-m-d'));
        }
        else{
            $paras['etime'] .= ' 23:59:59';
            $paras['etime'] = strtotime($paras['etime']);
        }

        $where = [];
        $where['is_name_time'] = ['BETWEEN',[$paras['stime'],$paras['etime']]];
        $where['diamond'] = ['eq',$diamond]; //渠道商编号

        $tmp = db('member')->field('COUNT(uid) as real_amount')->where($where)->find();
//        echo db('tongji')->getLastSql();die;
        $data['reals'] = $tmp['real_amount'];


    }


    //active_rate  激活率  =  激活数 / 注册数
    public function active_rate(&$data){

        if($data['regs']){
            $data['act_rate'] = $data['acts'] / $data['regs'];
        }
        else{
            $data['act_rate'] = 0;
        }

    }


    //实名率   reals_rate  = 实名 /  注册
    public function reals_rate(&$data){
        if($data['regs']){
            $data['reals_rate'] = $data['reals'] / $data['regs'];
        }
        else{
            $data['reals_rate'] = 0;
        }
    }



    //产品统计列表  ID, 产品名  , UV
    /* 表： go_shop_dj  ，   go_shoplist*/
    public function productList(){

        //参数接收
        $title = input('get.title'); //产品名
        $stime = input('get.stime'); //开始时间
        $etime = input('get.etime'); //结束时间
        $is_detail = input('get.is_detail');  //是否展示 明细 1.展示.   空则不展示
        $page = input('get.page'); //页码
        $lines = input('get.lines'); //行数

        empty($page) && $page = 1;
        empty($lines) && $lines = 20;

        $limit = ($page - 1) * $lines . ',' . $lines;    //分页

        $field = "id,title";
//        $join = [
//            ['go_shoplist b','a.shopid=b.id','LEFT'],
//        ];

        $where = [];
        if($title){
            $where['title'] = ['eq',$title];
        }


        $res = db('shoplist')
            ->alias('a')
            ->field($field)
            ->where($where)
            ->limit($limit)
            ->select();


        $total = db('shoplist')
            ->alias('a')
            ->field('COUNT(id) as t')
            ->where($where)
            ->find();

        if($res){
            foreach($res as &$v){

                    $this->productUV($v,$stime,$etime,$is_detail);  //可以用 静态变量去传递


            }//foreach

        }

//        dump($res);die;

        $arr = [];
        $arr['rows'] = $res;
        $arr['total'] = $total['t'];
        $arr['result'] = 1;
        $arr['tips'] = '查询成功！';

        $arr = json_encode($arr,320);
        exit($arr);

    }

    //产品UV
    public function productUV(&$data,$stime,$etime,$is_detail){

        $where=[];
        if($data){
            $where['shopid'] = ['eq',$data['id']];
        }

        if($stime && $etime){
            $where['date'] = ['BETWEEN',[ $stime,$etime ]];
        }else{
            $stime = date('Y-m-d');
//            $etime = date('Y-m-d');
            $where['date'] = ['eq',$stime];
        }


        $is_detail=1;
        //统计产品 UV
        if($is_detail){
            $field = "count(distinct uid) as uv,date";
        }else{
            $field = "count(distinct uid) as uv";
        }



        $res = db('shop_dj')->field($field)->where($where)->find();


        if(isset($res)){

            $data['uv'] = $res['uv'];

        }


        if(isset($res)){
            $data['date'] = isset($res['date']) ? $res['date'] : '';
        }


    }



    /* 流量漏斗列表   funcUseStatisticList
        表： go_tj
        列： ID  点击率
    */
    public function funcUseStatisticList(){
/*        if($_REQUEST['posttimestart']==''){
            $_REQUEST['posttimestart'] = date('Y-m-d 00:00:00');
        }
        $start = $_REQUEST['posttimestart'];


        if($_REQUEST['posttimeend']==''){
            $_REQUEST['posttimeend'] = date('Y-m-d 24:00:00');
        }
        $end = $_REQUEST['posttimeend'];

        if($_REQUEST['name']!=''){
            $where = ' AND agentid ="'.trim($_REQUEST['name']).'"';
        }else{
            $where = '';
        }*/


        /*SELECT
            `type`,COUNT(id) as cid
          FROM go_tj
          WHERE
            datetime>='".$start."'
          AND
            datetime<='".$end."' ".$where."
          GROUP BY
            `type`
          ORDER BY
            `type`
          ASC

        */
        $stime = input('get.stime');
        $etime = input('get.etime');
        /*$page = input('get.page'); //页码
        $lines = input('get.lines'); //行数*/


        //暂不考虑分页
        /*empty($page) && $page = 1;
        empty($lines) && $lines = 20;
        $limit = ($page - 1) * $lines . ',' . $lines;    //分页*/

        empty($stime) && $stime = date('Y-m-d 00:00:00');
        empty($etime) && $etime = date('Y-m-d 23:59:59');

        $res = db('tj')->query("SELECT
                            `type`,COUNT(id) as pv
                          FROM go_tj
                          WHERE
                            datetime>='".$stime."'
                          AND
                            datetime<='".$etime."'
                          GROUP BY
                            `type`
                          ORDER BY
                            `type`
                          "
        );


        foreach($res as &$v){
            switch ($v['type']){
                case 1: $v['type'] = '访问注册页';break;
                case 2: $v['type'] = '点击获取验证码';break;
                case 3: $v['type'] = '点击注册';break;
                case 4: $v['type'] = '访问下载页面';break;
                case 5: $v['type'] = '访问IOS证书信任页面';break;
                case 6: $v['type'] = '下载android版';break;
                case 7: $v['type'] = '下载IOS版';break;
                default:$v['type'] = '未知操作';break;

            }//switch
        }


        $arr=[];//初始化
        if($res && $res!==false){


            $arr = [];
            $arr['rows'] = $res;
            $arr['result'] = 1;
            $arr['tips'] = '查询成功！';

            $arr = json_encode($arr,320);
            echo $arr;
        }

        elseif(empty($res)){
            $arr = [];
            $arr['rows'] = '';
            $arr['result'] = 1;
            $arr['tips'] = '查询成功！暂无数据哟~~ 巧妇难为无米之炊，请客官耐心等候数据~';

            $arr = json_encode($arr,320);
            echo $arr;
        }

        else{
            $arr = [];
            $arr['rows'] = [];
            $arr['result'] = 2;
            $arr['tips'] = '查询失败， 请稍后再试！';
            $arr = json_encode($arr,320);
            echo $arr;
        }



    }//func


    /*  列： 操作名称 ， 点击率
        功能：操作统计
        表： go_statistics
    */
    public function userActionStatisticList(){

        /*
        $this->db->GetList("
            SELECT
                `function`,COUNT(id) AS cid
            FROM  (
                        select * from go_statistics
                            WHERE `time`>='".$start."'
                            AND `time`<='".$end."')
            as a
            GROUP BY `function`");
        */


        //接收参数
        $stime = input('get.stime');
        $etime = input('get.etime');


        if(empty($stime)){
            $stime = date('Y-m-d').' 00:00:00';
            $stime = strtotime($stime);
//            echo $stime;die;

        }

        if(empty($etime)){
            $etime = date('Y-m-d'). ' 23:59:59';
            $etime = strtotime($etime);
        }


        //tp 中的子查询
        $pdo = new \think\db\Query();

        $res = $pdo->query("SELECT
                                      `function`,COUNT(id) AS pv
                                    FROM  (
                                                select * from go_statistics
                                                    WHERE `time`>='".$stime."'
                                                    AND `time`<='".$etime."')
                                    as a
                                    GROUP BY `function`");



        //转化数据  function
        if($res){
            foreach($res as &$v){

                switch ($v['function']){
                    case 'bootpage': $v['function'] = '启动APP';break;
                    case 'get_area': $v['function'] = '地址编辑';break;
                    case 'get_person': $v['function'] = '获取实名信息';break;
                    case 'get_user': $v['function'] = '获取个人信息';break;
                    case 'get_version': $v['function'] = '获取最新版本';break;
                    case 'history': $v['function'] = '浏览记录';break;
                    case 'login': $v['function'] = '登陆操作';break;
                    case 'logout': $v['function'] = '登出操作';break;
                    case 'set_person': $v['function'] = '编辑实名信息';break;
                    case 'to_url': $v['function'] = '点击产品';break;
                    default: $v['function'] = '未知功能';break;
                }//switch
            }//foreach
        }


        $msg = [];//初始化  消息盒子
        if(!empty($res) && $res!==false){
            $msg['rows'] = $res;
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }
        elseif(empty($res)){
            $msg['rows'] = [];
            $msg['result'] = 1;
            $msg['tips'] = '查询成功！但是并没有数据哟~';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }
        else{
            $msg['rows'] = [];
            $msg['result'] = 2;
            $msg['tips'] = '查询失败！';
            //返回前端
            $info = json_encode($msg,320);
            exit($info);
        }


    }//操作统计  func




    /*首页统计显示
        可通过定时任务 异步获取
    */
    public function statisticMainPage(){


        $stime = input('get.stime');
        $etime = input('get.etime');

        /*echo strtotime('2019-04-04 00:00:00');
        die;*/

        if(empty($stime) && empty($etime)) {//
            $cached = new Redis();

            $shuju = $cached->get('falcon_deng');  //获取json 字符串数据
//
            if (!empty($shuju)) {

                $shuju2 = json_decode($shuju,true); //转为数组
                $arr = [];
                $arr['rows'] = $shuju2;
                $arr['result'] = 1;
                $arr['tips'] = '查询成功！Redis';
                $arr = json_encode($arr, JSON_UNESCAPED_UNICODE);
                exit($arr);
            }
        }


        empty($stime) && $stime = date('Y-m-d');
        empty($etime) && $etime = date('Y-m-d');

        /*如果没有传参数  默认从今天开始算 往前推6天*/

//        $stime = '1997-05-16';
//        $etime = '1997-05-23';  //test

        $ret = []; //消息盒子 返回给前端用的 包装信息数组

        $diff = (strtotime($etime) - strtotime($stime)) / 86400;
        if(($diff > 6 ) && ($diff < 0) ){
            //大于7天的查询  不受理
            $ret = [

                'result' => 2,
                'tips' => '时间范围不能大于7天 或 时间差不能小于0'
            ];

            exit(json_encode($ret,320));
        }

//        echo date('Y-m-d',strtotime("$etime -1 day")); //测试


        /*
         * 如果有输入日期 且范围不大于7天 则用 while 循环判断
         * */

        if(empty($etime)){
            $day0 = date('Y-m-d');
        }else{

            $day0 = $etime;
        }

        $day1 =  date('Y-m-d',strtotime("$day0 -1 day"));
        $day2 = date('Y-m-d',strtotime("$day0 -2 day"));
        $day3 = date('Y-m-d',strtotime("$day0 -3 day"));
        $day4 = date('Y-m-d',strtotime("$day0 -4 day"));
        $day5 = date('Y-m-d',strtotime("$day0 -5 day"));
        $day6 = date('Y-m-d',strtotime("$day0 -6 day"));
        // echo '当天: ' . $day.'<br>';
        // echo '昨天: ' . $yestoday.'<br>';
        // echo '前天: ' . $thedaybfyestoday.'<br>';die;
        

        $rec = [];  //初始化 接收处理好的数据
        
        $where_day = [];  //初始化 日期条件

        for($i=0;$i<=6;$i++){
            $where_day['date'] = ${'day' . $i};  //当前日期  变量的变量
            //需要转成时间戳
            $day_begin = strtotime($where_day['date'] . ' 00:00:00');  //时分秒
            $day_end = strtotime($where_day['date'] . ' 23:59:59');

            
            /* 今天UV 开始*/
            $uv = db('tongji')->field('COUNT(DISTINCT ip) as uv')->where($where_day)->find();
            
            /* 今天注册数 开始*/
            $where_regs['time'] = ['BETWEEN',[$day_begin,$day_end]];
            $regs = db('member')->field('COUNT(uid) as reg_amount')->where($where_regs)->find();

            /* 今天激活数 开始         //如果区分 渠道商， 就加上 diamond 条件*/
            $where_acts['first_login_time'] = ['BETWEEN',[$day_begin,$day_end]];
            $acts = db('member')->field('COUNT(uid) as act_amount')->where($where_acts)->find();

            /*今日实名量 */
            $where_reals['is_name_time'] = ['BETWEEN',[$day_begin,$day_end]];
            $reals = db('member')->field('COUNT(uid) as real_amount')->where($where_reals)->find();

            /*注册率   = 注册数/UV  */
            if($regs['reg_amount'] && $uv['uv']!=0){
                $reg_rate = ($regs['reg_amount'] / $uv['uv'] ) . '%';
            }else{
                $reg_rate = 0;
            }


            /*激活率   = 激活数/注册数  */
            if($regs['reg_amount'] && $regs['reg_amount']!=0){
                $act_rate = ($acts['act_amount'] / $regs['reg_amount'] ) . '%';
            }else{
                $act_rate = 0;
            }

            /*实名率   = 实名/注册  */
            if($reals['real_amount'] && $regs['reg_amount']!=0){
                $real_rate = ($reals['real_amount'] / $regs['reg_amount'] ) . '%';
            }else{
                $real_rate = 0;
            }

            //数组结果组装
            $rec['day' . $i]['time'] =  $where_day['date'];  //日UV
            $rec['day' . $i]['uv'] =  $uv['uv'];  //日UV
            $rec['day' . $i]['zhuce'] =  $regs['reg_amount']; //注册数
            $rec['day' . $i]['acts'] =  $acts['act_amount'];    //激活数
            $rec['day' . $i]['reals'] = $reals['real_amount'];  //实名数
            $rec['day' . $i]['reg_rate'] = $reg_rate;  //注册率
            $rec['day' . $i]['act_rate'] = $act_rate; //激活率
            $rec['day' . $i]['real_rate'] = $real_rate; //实名率
           
        }

//        dump($rec);die;
        

        $ret['result'] = 0;
        $ret['rows'] = $rec;
        $ret['tips'] = '查询成功! Not Redis';
        if($rec){
            $ret['result'] = 1;
        }

        $hi = json_encode($ret,320) ;
        exit($hi);

    }//func statisticMainPage


    //首页图表
    public function main_table(){


        $stime = input('get.stime');
        $etime = input('get.etime');


        //默认使用缓存技术
        if(empty($stime) && empty($etime) || $etime == date('Y-m-d')){
            $cached = new Redis();

            if($cached->get('falcon_deng')){
                $data = $cached->get('falcon_deng');
                $data = json_decode($data,true);

                $data2 = [];
                foreach($data as $k=>&$v){
                    $data2[] = $v;
                }

                $arr = [];
                $arr['rows'] = $data2;
                $arr['result'] = 1;
                $arr['tips'] = '查询成功！Redis';
                $arr = json_encode($arr,JSON_UNESCAPED_UNICODE);
                exit($arr);
            }
        }


        empty($stime) && $stime = date('Y-m-d');
        empty($etime) && $etime = date('Y-m-d');

        /*如果没有传参数  默认从今天开始算 往前推6天*/

//        $stime = '1997-05-16';
//        $etime = '1997-05-23';  //test

        $ret = []; //消息盒子 返回给前端用的 包装信息数组

        $diff = (strtotime($etime) - strtotime($stime)) / 86400;
        if(($diff > 6) && ($diff < 0)){
            //大于7天的查询  不受理
            $ret = [

                'result' => 2,
                'tips' => '时间范围不能大于7天 或 时间差不能小于0'
            ];

            exit(json_encode($ret,320));
        }

//        echo date('Y-m-d',strtotime("$etime -1 day")); //测试


        /*
         * 如果有输入日期 且范围不大于7天 则用 while 循环判断
         * */

        if(empty($etime)){
            $day[0] = date('Y-m-d');
        }else{

            $day[0] = $etime;
        }

        $day[1] =  date('Y-m-d',strtotime("$day[0] -1 day"));
        $day[2] = date('Y-m-d',strtotime("$day[0] -2 day"));
        $day[3] = date('Y-m-d',strtotime("$day[0] -3 day"));
        $day[4] = date('Y-m-d',strtotime("$day[0] -4 day"));
        $day[5] = date('Y-m-d',strtotime("$day[0] -5 day"));
        $day[6] = date('Y-m-d',strtotime("$day[0] -6 day"));
        // echo '当天: ' . $day.'<br>';
        // echo '昨天: ' . $yestoday.'<br>';
        // echo '前天: ' . $thedaybfyestoday.'<br>';die;


        $rec = [];  //初始化 接收处理好的数据

        $where_day = [];  //初始化 日期条件

        for($i=0;$i<=6;$i++){
            $where_day['date'] = $day[$i];  //当前日期  变量的变量


            /* 今天UV 开始*/
            $uv = db('tongji')->field('COUNT(DISTINCT ip) as uv')->where($where_day)->find();

            /* 今天注册数 开始*/
            $where_regs = [];
            $where_regs['time'] = ['BETWEEN',[strtotime($where_day['date'] . ' 00:00:00') , strtotime($where_day['date'] . ' 23:59:59')]];
            $regs = db('go_member')->field('COUNT(uid) as reg_amount')->where($where_regs)->find();

            /* 今天激活数 开始         //如果区分 渠道商， 就加上 diamond 条件*/
            $acts = db('member')->field('COUNT(uid) as act_amount')->where($where_day)->find();

            /*今日实名量 */
            $reals = db('member')->field('COUNT(uid) as real_amount')->where($where_day)->find();

            /*注册率   = 注册数/UV  */
            if($regs['reg_amount'] && $uv['uv']!=0){
                $reg_rate = ($regs['reg_amount'] / $uv['uv'] ) . '%';
            }else{
                $reg_rate = 0;
            }


            /*激活率   = 激活数/注册数  */
            if($regs['reg_amount'] && $regs['reg_amount']!=0){
                $act_rate = ($acts['act_amount'] / $regs['reg_amount'] ) . '%';
            }else{
                $act_rate = 0;
            }

            /*实名率   = 实名/注册  */
            if($reals['real_amount'] && $regs['reg_amount']!=0){
                $real_rate = ($reals['real_amount'] / $regs['reg_amount'] ) . '%';
            }else{
                $real_rate = 0;
            }

            //数组结果组装
            $rec[$i]['time'] =  $where_day['date'];  //日UV
            $rec[$i]['uv'] =  $uv['uv'];  //日UV
            $rec[$i]['zhuce'] =  $regs['reg_amount']; //注册数
            $rec[$i]['acts'] =  $acts['act_amount'];    //激活数
            $rec[$i]['reals'] = $reals['real_amount'];  //实名数
            $rec[$i]['reg_rate'] = $reg_rate;  //注册率
            $rec[$i]['act_rate'] = $act_rate; //激活率
            $rec[$i]['real_rate'] = $real_rate; //实名率

        }


        $ret['result'] = 0;
        $ret['rows'] = $rec;
        $ret['tips'] = '查询成功。 Not Redis';
        if($rec){
            $ret['result'] = 1;
        }

        $hi = json_encode($ret,320) ;
        echo $hi;

    }//func statisticMainPage



    /* 用户对功能区的访问统计*/
    public function uri_statistic(){
        $uid = input('post.uid');
        $ip = input('post.ip');
        $function = input('post.function');
        $uri_name = input('post.uri_name');  //功能uri

        $data = [];
        $data['uid'] = trim($uid);
        $data['ip'] = trim($ip);
        $data['function'] = trim($function);
        $data['uri_name'] = trim($uri_name);


        $where = [];
        $where['uid'] = $uid;
        $where['ip'] = $ip;
        $where['time'] = time();
        $where['date'] = date('Y-m-d');
        db('tongji')->insert($where);
        $res = db('statistic')->insert();
        $box = [];
        if($res){
            $box['result'] = 1;
            $box['tips'] = '统计成功';
        }
        else{
            $box['result'] = 2;
            $box['tips'] = '网络拥堵， 统计失败';
        }

        exit(json_encode($box,320));

    }






}